package com.example.jchuah.tiles;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;


public class MyActivity extends Activity implements View.OnTouchListener, View.OnDragListener, FlingDetector.FlingListener {

    int numRows = 4;
    int numCols = 4;



    TileGridLayout grid;
    Random randGen = new Random();
    FlingDetector flingDetector = new FlingDetector(this);
    GestureDetector gestureDetector;

    public void checkVictory() {
        int tilecount = 0;
        for (int row = 0; row < numRows; row++) {
            for (int col = 0; col < numCols; col++) {


                RowColPair p = new RowColPair();
                p.row = row;
                p.col = col;

                View theTile = grid.getTile(p);
                        if (theTile.getTag().equals(R.layout.tile)) {
                            tilecount++;
                        }
                        if (theTile.getTag().equals(R.layout.tile2)) {
                            tilecount++;
                        }
                        if (theTile.getTag().equals(R.layout.tile3)) {
                            tilecount++;
                        }
                        if (theTile.getTag().equals(R.layout.tile4)) {
                            tilecount++;
                        }
                        if (theTile.getTag().equals(R.layout.tile5)) {
                            tilecount++;
                        }


                    }
                }
        if (tilecount == 5) {
            startActivity(new Intent(this, MyActivity2.class));
        }
            }



    public void compareLocations(RowColPair source, RowColPair destination) {
        if (  grid.isValidRowColPair( destination  )   ) {
            View flung = grid.getTile(source);
            View target = grid.getTile(destination);

            if (flung.getTag().equals(target.getTag())) {
                // tiles are the same
            }
        }
    }

    public void onFlingLeft(View view) {
        Log.d("fling event", "left");
        Toast.makeText(this, "join my company!", Toast.LENGTH_LONG).show();
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair leftOf = coordinate.getLeft();
        if (grid.isValidRowColPair(leftOf)) {
            View justRemoved = grid.removeTile(leftOf);
        grid.removeTile(coordinate);
            grid.addTile(leftOf , view);
        if (justRemoved.getTag().equals(view.getTag())) {
            View blanktile = getLayoutInflater()
                    .inflate(R.layout.tile0, null);
            blanktile.setTag(R.layout.tile0);
            grid.addTile(coordinate, blanktile);


        } else {

            grid.addTile(coordinate, justRemoved);


        }
        }
        checkVictory();
    }

    public void onFlingRight(View view) {
        Log.d("fling event", "right");
        Toast.makeText(this, "join my company!", Toast.LENGTH_LONG).show();
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair rightOf = coordinate.getRight();
        if (grid.isValidRowColPair(rightOf)) {
            View justRemoved = grid.removeTile(rightOf);
            grid.removeTile(coordinate);
            grid.addTile(rightOf, view);

            if (justRemoved.getTag().equals(view.getTag())) {
                View blanktile = getLayoutInflater()
                        .inflate(R.layout.tile0, null);
                blanktile.setTag(R.layout.tile0);
                grid.addTile(coordinate, blanktile);
            } else {
                grid.addTile(coordinate, justRemoved);


            }
        }
        checkVictory();
    }

    public void onFlingUp(View view) {
        Log.d("fling event", "up");
        Toast.makeText(this, "join my company!", Toast.LENGTH_LONG).show();
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair aboveOf = coordinate.getAbove();
        if (grid.isValidRowColPair(aboveOf)) {
            View justRemoved = grid.removeTile(aboveOf);
            grid.removeTile(coordinate);
            grid.addTile(aboveOf, view);
            if (justRemoved.getTag().equals(view.getTag())) {
                View blanktile = getLayoutInflater()
                        .inflate(R.layout.tile0, null);
                blanktile.setTag(R.layout.tile0);
                grid.addTile(coordinate, blanktile);
            } else {
                grid.addTile(coordinate, justRemoved);
            }
        }
        checkVictory();
    }

    public void onFlingDown(View view) {
        Log.d("fling event", "down");
        Toast.makeText(this, "join my company!", Toast.LENGTH_LONG).show();
        RowColPair coordinate;
        coordinate = grid.getRowColumn(view);
        RowColPair belowOf = coordinate.getBelow();
        if (grid.isValidRowColPair(belowOf)) {
            View justRemoved = grid.removeTile(belowOf);
        grid.removeTile(coordinate);
            grid.addTile(belowOf , view);
        if (justRemoved.getTag().equals(view.getTag()))
        {
if (justRemoved.getTag().equals(R.layout.tile));
            View blanktile = getLayoutInflater()
                    .inflate(R.layout.tile0, null);
            blanktile.setTag(R.layout.tile0);
            grid.addTile(coordinate, blanktile);

        } else {
            grid.addTile(coordinate, justRemoved);


        }
        }
        checkVictory();
    }

    public void generateGrid() {
        int type = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();


        for (int i = 0; i < 4; i++) {
            list.add(R.layout.tile);
            list.add(R.layout.tile2);
            list.add(R.layout.tile3);
            list.add(R.layout.tile4);
            list.add(R.layout.tile5);
        }

        for (int row = 0; row < numRows; row++){
            for (int col = 0; col < numCols; col++) {
                int id = 0;
                id = list.remove(randGen.nextInt(list.size()));

                View tile = getLayoutInflater()
                        .inflate(id, null);
                tile.setTag(id);

                tile.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View view, MotionEvent motionEvent) {
                        flingDetector.touched = view;
                        return gestureDetector.onTouchEvent(motionEvent);
                    }
                });
                grid.addTile(row, col, tile);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        grid = (TileGridLayout)findViewById(R.id.gridView);
        generateGrid();
        gestureDetector = new GestureDetector(this, flingDetector);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        Toast.makeText(this, "lol", Toast.LENGTH_LONG).show();
        return false;
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        Toast.makeText(this, "join my company!", Toast.LENGTH_LONG).show();
        return false;
    }

}
